package com.tang.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tang.common.api.UserFeignClient;
import com.tang.common.entity.community.Article;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.community.service.ArticleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserFeignClient remoteAdminService;


    @RequestMapping("/articleList")
    public Result getArticleList(String keywords, Integer currentPage, Integer pageSize, Integer hasAuth) {
        PageInfo<Article> list = PageInfo.of(articleService.listArticle(keywords, currentPage, pageSize, hasAuth));
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/myArticleList")
    public Result getArticleList(Integer currentPage, Integer pageSize, String userName) {
        PageInfo<Article> list = PageInfo.of(articleService.listMyArticle(currentPage, pageSize, userName));
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/addArticle")
    public Result addArticle(String userName, String titleinput, String editorContent, String articleType) {
        if (titleinput != null && editorContent != null && articleType != null) {
            Article article  = new Article();
            article.setArticleType(articleType);
            article.setArticleContent(editorContent);
            article.setArticleTitle(titleinput);
            article.setUserName(userName);
            boolean save = articleService.save(article);
            if(save){
                return Result.success("发布成功");
            }
        }
        return Result.error(RecruitExceptionEnum.PUBLISH_FAILED);
    }

    @RequestMapping("/editArticle")
    public Result editArticle(Integer articleId,String titleinput, String editorContent, String articleType) {
        Article article = articleService.getById(articleId);
        if(article!=null){
            article.setArticleType(articleType);
            article.setArticleContent(editorContent);
            article.setArticleTitle(titleinput);
            boolean save = articleService.updateById(article);
            if(save){
                return Result.success("发布成功");
            }
        }
        return Result.error(RecruitExceptionEnum.PUBLISH_FAILED);
    }


    @RequestMapping("/getArticle")
    public Result getArticle(Integer articleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("article_id", articleId);
        queryWrapper.eq("delete_flag", 1);
        queryWrapper.eq("state", 1);
        Article article = articleService.getOne(queryWrapper);
        if (article != null) {
            return Result.success(article);
        }
        return Result.error(RecruitExceptionEnum.NO_DATA);
    }

    @RequestMapping("/delArticle")
    public Result delArticleById(String articleId) {
        Article article = articleService.getById(articleId);
        article.setDeleteFlag(0);
        boolean result = articleService.updateById(article);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }

    @ApiOperation("审核文章")
    @RequestMapping("/auditArticle")
    public Result auditArticle(Integer articleId, String res) {
        String result = articleService.auditArticle(articleId, res);
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error(RecruitExceptionEnum.AUDIT_FAILED);
        }
    }

    @RequestMapping("/articleCount")
    public Integer getArticleCount() {
        return articleService.count(null);
    }


}

